package cn.cyber.wxfire.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class JDBCController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @GetMapping("/user/list")
    public List<Map<String,Object>> userList()
    {
        String sql = "select * from student";
        List<Map<String,Object>> s=jdbcTemplate.queryForList(sql);
        return s;

    }

    @GetMapping("/user/add")
    public String addUser()
    {
        String sql="insert into student(name,age) values('sss',18)";
        jdbcTemplate.update(sql);
        return "add ok";
    }

    @GetMapping("/user/update/{id}")
    public String updateUser(@PathVariable("id") int id)
    {
        String sql="update student set name=? where id="+id;
        String[] params={"alice"};
        jdbcTemplate.update(sql,params);
        return "update ok";
    }

    @GetMapping("/user/delete/{id}")
    public String deleteUser(@PathVariable("id") int id)
    {
        String sql="delete from student where id="+id;
        jdbcTemplate.update(sql);
        return "update ok";
    }
}
